package com.boxedinsoftware.betamax.dialer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TwoLineListItem;
 
@SuppressWarnings("deprecation")
public class ContactListerLegacy extends ListActivity implements Runnable {
 
    String[] columns ;
    
    private List<Contact> contacts = null;
    private Contact con;
    private ContactArrayAdapter cAdapter;
    private ProgressDialog prog = null;
    private Context thisContext = this;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prog = ProgressDialog.show(this, "Please Wait...", "Populating Contact List", true, false);
        Thread thread = new Thread(this);
        thread.start();
        
        
    }
    
    public void run() {
        if (contacts == null)
        {
            contacts = fillContactsList();
 
        }
        handler.sendEmptyMessage(0);
    }
 
    @SuppressWarnings("unchecked")
	private List<Contact> fillContactsList() {
        List<Contact> tmpList = new ArrayList<Contact>();
        ContentResolver cr = getContentResolver();
        Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
        while(c.moveToNext()){
            String ContactID = c.getString(c.getColumnIndex(People._ID));
            String name = c.getString(c.getColumnIndex(People.DISPLAY_NAME));
            //String hasPhone =c.getString(
                    //c.getColumnIndex(People.);
          // if(Integer.parseInt(hasPhone) == 1){
            	Cursor phoneCursor = cr.query(
        				Contacts.Phones.CONTENT_URI, 
        				null, 
        				Contacts.Phones.PERSON_ID +" = ?", 
        				new String[]{ContactID}, null);
                while(phoneCursor.moveToNext()){
                    String number = phoneCursor.getString(phoneCursor.getColumnIndex(People.NUMBER));
                    String type = phoneCursor.getString(phoneCursor.getColumnIndex(People.TYPE));
                    con = new Contact();
                    con.setName(name);
                    type = getType(type);
                    con.setNumber(number + " (" + type + ")");
                    tmpList.add(con);
                }
                phoneCursor.close();
           // }
 
        }
        c.close();
        Collections.sort(tmpList);
        return tmpList;
    }
 
    
    	

	private String getType(String type) {
		int x = Integer.parseInt(type);
		if(x == Phone.TYPE_ASSISTANT)
			return "Assistant";
		else if (x == Phone.TYPE_CALLBACK)
			return "Callback";
		else if (x == Phone.TYPE_CAR)
			return "Car";
		else if (x == Phone.TYPE_COMPANY_MAIN)
			return "Company";
		else if (x == Phone.TYPE_FAX_HOME)
			return "Fax Home";
		else if (x == Phone.TYPE_FAX_WORK)
			return "Fax Work";
		else if (x == Phone.TYPE_HOME)
			return "Home";
		else if (x == Phone.TYPE_ISDN)
			return "ISDN";
		else if (x == Phone.TYPE_MAIN)
			return "Main";
		else if (x == Phone.TYPE_MMS)
			return "MMS";
		else if (x == Phone.TYPE_MOBILE)
			return "Mobile";
		else if (x == Phone.TYPE_OTHER)
			return "Other";
		else if (x == Phone.TYPE_OTHER_FAX)
			return "Other Fax";
		else if (x == Phone.TYPE_PAGER)
			return "Pager";
		else if (x == Phone.TYPE_RADIO)
			return "Radio";
		else if (x == Phone.TYPE_TELEX)
			return "Telex";
		else if (x == Phone.TYPE_TTY_TDD)
			return "TTY TDD";
		else if (x == Phone.TYPE_WORK)
			return "Work";
		else if (x == Phone.TYPE_WORK_MOBILE)
			return "Work Mobile";
		else if (x == Phone.TYPE_WORK_PAGER)
			return "Work Pager";
		else if (x == Phone.TYPE_CUSTOM)
			return "Other";
		
		return "Other";
	}




	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            prog.dismiss();
            cAdapter = new ContactArrayAdapter(thisContext, R.layout.listitemlayout, contacts);
            getListView().setFastScrollEnabled(true);
            setListAdapter(cAdapter);
 
        }
    };
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        TextView label = ((TwoLineListItem) v).getText2();
        String phoneNumber = label.getText().toString();
        this.getIntent().putExtra("returnNumber", phoneNumber);
        setResult(Activity.RESULT_OK, this.getIntent());
		
		
	    finish();
    }
}
